home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / torus-sr.tar / torus-sr / torus / good.c next >
Text File  |  1991-06-19  |  4KB  |  158 lines

  1. # include "robots.h"
  2.  
  3. /*
  4.  * good.c: Figure out all possible good moves.
  5.  * This piece of code use to be trivial - then fast robots came
  6.  * and moveable heaps and its now an AI expert system in its own right!
  7.  */
  8.  
  9. good_moves ()
  10. {
  11.   register int test_x, test_y;
  12.   register char *m, *a;
  13.   static char moves[] = "hjklyubn.a";
  14.   static char ans[sizeof (moves)];
  15.   char savebuf[9], savechar;
  16.   int i,j, notsilly;
  17.  
  18.   a = ans;
  19.   for(m = moves; *m != 'a'; m++) {
  20.     test_x = hbound(my_y+yinc(*m),my_x+xinc(*m));
  21.     test_y = vbound(my_y+yinc(*m),my_x+xinc(*m));
  22.     move(test_y,test_x);
  23.     switch(inch()) {
  24.       case ' ':
  25.       case DOT:
  26.       case ME:
  27.         if( isgood(test_y, test_x) )
  28.           *a++ = *m;
  29.         break;
  30.       case SCRAP:
  31.         if (moveable_heaps) {
  32.         int nscrap_x,nscrap_y;
  33.         nscrap_x = hbound(test_y +yinc(*m),test_x +xinc(*m));
  34.         nscrap_y = vbound(test_y +yinc(*m),test_x +xinc(*m));
  35.         move(nscrap_y,nscrap_x);
  36. /*    if (inch()==DOT) {
  37.           addch(SCRAP);
  38.           if (isgood(test_y,test_x))
  39.             *a++ = *m;
  40.           move(nscrap_y,nscrap_x);
  41.           addch(DOT);
  42.           }
  43.         if (inch()==' ') {
  44.           addch(SCRAP);
  45.           if (isgood(test_y,test_x))
  46.             *a++ = *m;
  47.           move(nscrap_y,nscrap_x);
  48.           addch(' ');
  49.       } */ /* rfs -- rewritten to eliminate bug in wimpy mode */
  50.     if ((savechar=inch())==DOT || savechar==' ') {
  51.       addch(SCRAP);
  52.       if (isgood(test_y,test_x))
  53.         *a++ = *m;
  54.       move(nscrap_y,nscrap_x);
  55.       addch(savechar);
  56.       } /* rfs -- end of replacement code */
  57.         }
  58.         break;
  59.     }
  60.   }
  61.   /* now lets do the antimatter case */
  62.   if (free_teleports) {
  63.   notsilly = 0;  /* silly to do it if not get robots */
  64.   for(i= -1;i <= 1; i++)
  65.     for(j= -1; j<= 1; j++) {
  66.       tmove(my_y+i,my_x+j);
  67.       savechar = inch();
  68.       if (savechar == FROBOT || savechar == ROBOT) {
  69.         savebuf[ (i+1)*3 +j +1] = savechar;
  70.         addch(' ');
  71.         notsilly = 1;
  72.         }
  73.       else
  74.         savebuf[ (i+1)*3 +j +1] = ' ';
  75.       }
  76.   if( notsilly && isgood(my_y,my_x) )
  77.     *a++ = *m;
  78.  
  79.   for(i= -1;i <= 1; i++)
  80.     for(j= -1; j<= 1; j++) {
  81.       tmove(my_y+i,my_x+j);
  82.       savechar = savebuf[ (i+1)*3 +j +1];
  83.       if (savechar == FROBOT || savechar == ROBOT)
  84.         addch(savechar);
  85.       }
  86.   }
  87.   *a = 0;
  88.   if(ans[0]) {
  89.     a = ans;
  90.   } else {
  91.     a = "Forget it!";
  92.   }
  93.   mvprintw(LINES-1,MSGPOS,"%-*.*s",RVPOS-MSGPOS,RVPOS-MSGPOS,a);
  94. }
  95.  
  96. isgood(ty,tx)
  97. register int tx, ty;
  98. {
  99.   register int x, y;
  100.  
  101.   for(x = -2; x <= 2; x++) {
  102.     for(y = -2; y <= 2; y++) {
  103.       if ( abs(x) > 1 || abs(y) > 1 ) { /* we are 2 steps out */
  104.         if( blocked(ty, tx, y, x))
  105.           continue;
  106.         tmove(ty+y,tx+x);
  107.         if ( abs(x) == 2 && abs(y) == 2
  108.               && inch() == FROBOT){
  109.           return FALSE;  }
  110.         if ( x == -1 && scan(ty+y, tx+x, 0, 1) )
  111.           return FALSE;
  112.         if ( y == -1 && scan(ty+y, tx+x, 1, 0) )
  113.           return FALSE;
  114.       } /* outer perimeter checked */
  115.       else {
  116.         tmove(ty+y,tx+x);
  117.         switch(inch()) {
  118.           case FROBOT:
  119.           case ROBOT: /* robot next to you */
  120.             return FALSE;
  121.         }
  122.       }
  123.     }
  124.   }
  125.   return TRUE;
  126. }
  127.  
  128. scan(y, x, yi, xi) /* scan along this line looking for collision conditions */
  129. int x, y, xi, yi;
  130. {
  131.   int rcount = 0;
  132.   register int ctr;
  133.  
  134.   for(ctr = 0;ctr<=2;ctr++) {
  135.     tmove(y + (ctr*yi), x + (ctr*xi));
  136.     switch(inch()) {
  137.       case FROBOT:
  138.         rcount += 4;
  139.         break;
  140.       case ROBOT:
  141.         rcount ++;
  142.         break;
  143.     }
  144.   }
  145.   return rcount == 4;
  146. }
  147.  
  148. blocked(my, mx, y, x)
  149. register int  my, mx, y, x;
  150. {
  151.   if ( x < 0 ) x++;
  152.   if ( y < 0 ) y++;
  153.   if ( x > 0 ) x--;
  154.   if ( y > 0 ) y--;
  155.   tmove(my+y, mx+x);
  156.   return inch() == SCRAP;
  157. }
  158.